<!--
 * @Author: 白印潇 bai_yinxiao@163.com
 * @Date: 2023-03-09 15:32:47
 * @LastEditors: 白印潇 bai_yinxiao@163.com
 * @LastEditTime: 2023-03-27 17:14:08
 * @FilePath: /shortRent-ui/src/views/system/credit/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container">
    <div class="custom_card_0">
      <!-- 顶部搜索栏 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="small">
        <el-form-item label="征信包" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入征信包名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="租户" prop="tenantName">
          <el-input v-model="queryParams.tenantName" placeholder="请输入租户名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 新增 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>
    <!-- 征信包 -->
    <div>
      <el-table v-loading="loading" :data="basicList" stripe
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }" border>
        <template slot="empty">
          <el-empty :image-size="50" description="暂无征信包"></el-empty>
        </template>
        <el-table-column label="征信包" align="center" prop="name" show-overflow-tooltip />
        <el-table-column label="类型" align="center" prop="type">
          <template v-slot="{ row }">
            {{ row.type == 1 ? '企业' : '个人' }}
          </template>
        </el-table-column>
        <el-table-column label="介绍" align="center" prop="remark" min-width="120" show-overflow-tooltip />
        <el-table-column label="单位(元/次)" align="center" prop="price" />
        <el-table-column label="使用租户" align="center" prop="tenantList">
          <template v-slot="{ row }">
            {{ getRowTenantInfo(row) }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
          <template v-slot="{ row }">
            <el-button size="small" type="text" @click="handleDetail(row)">查看</el-button>
            <!-- 通过关联的租户列表是否为空判断是否可编辑|删除, 租户数为0时可编辑|删除 -->
            <template v-if="row.tenantList.length == 0">
              <el-button size="small" type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button size="small" type="text" @click="handleDelete(row)">删除</el-button>
            </template>


          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div v-show="total > 0" style="background-color: #fff;padding-bottom: 10px; margin-top: 10px;">
        <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </div>
    </div>

    <!-- 新增征信查询包 -->
    <add-package ref="addPackage" @success="refrehList"></add-package>
  </div>
</template>

<script>
import * as creditApi from '@/api/credit/credit';
import AddPackage from './components/addPackage.vue';
export default {
  name: 'CreditPackage',
  components: {
    AddPackage
  },
  data() {
    return {
      // 加载标识
      loading: false,
      // 总条数
      total: 0,
      // 征信包表格数据
      basicList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantName: '',
        name: ''

      }
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },
  methods: {
    // 搜索
    handleQuery() {
      this.refrehList();
    },
    // 重置
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.refrehList();
    },
    // 刷新列表
    refrehList() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询征信包信息 */
    getList() {
      this.loading = true
      creditApi.creditList(this.queryParams).then(res => {
        this.basicList = res.rows;
        this.total = res.total;
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 获取每个工单下的租户信息(因接口返回)
    getRowTenantInfo(row) {
      const tenantNames = [];
      row.tenantList.forEach((item) => {
        tenantNames.push(item.tenantName);
      })
      return tenantNames.join(',');
    },
    // 新增
    handleAdd() {
      this.$refs.addPackage.show(0);
    },

    // 查看
    handleDetail(item) {
      //1.通过name:query传参 参数会暴露在地址栏,密码等安全信息字段建议不要采用query传参
      // this.$router.push({ name: 'packageDetail', query: { id: item.id } })
      //2.通过path:query传参 参数会暴露在地址栏,密码等安全信息字段建议不要采用query传参
      // this.$router.push({ path: '/packageDetail', query: { id: item.id } })
      //3.通过 name:params传参  params传参必须配合name使用, 且如果在路由配置path里面没有定义接收的参数,页面刷新参数会丢失(敏感安全字段建议用params传参),地址栏不会出现参数名
      this.$router.push({ name: 'packageDetail', params: { id: item.id } })
    },
    //编辑
    handleEdit(item) {
      this.$refs.addPackage.show(1, item.id);
    },
    //删除
    handleDelete(item) {
      this.$confirm(`'确定删除征信包[${item.name}]吗?'`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        creditApi.deleteCredit(item.id).then(res => {
          if (res.code == 200) {
            this.refrehList();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }

        })
      })
    },


  },
}
</script>

<style scoped>
.customer_card_1 {
  background-color: #fff;
  padding: 30px;
  margin: 15px 0;
  display: flex;
  flex-direction: row;
  align-items: center;
}

/deep/ .table_header {
  background-color: #7aa1ea !important;
}
